/**
 * 
 */
package uk.ac.manchester.cs.graph;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EmptyStackException;
import java.util.Iterator;

/**
 * @author Pavel Klinov
 *
 * pklinov@cs.man.ac.uk, pklinov@clarkparsia.com
 * 
 * Nov 7, 2008
 * 
 * The _real_ stack. Not the horrible Sun's implementation (see 
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4475301)
 */
public class StackImpl<T> extends ArrayList<T> implements Stack<T> {

	private static final long	serialVersionUID	= 6710851383021328998L;

	public Entry<T> find(T object) {

		int depth = 0;
		
		for (T element : this) {
			
			if (element.equals( object )) {
				
				return new Entry<T>(element, depth);
			}
			
			depth++;
		}
		
		return null;
	}
	
	public Entry<T> find(T object, Comparator<T> comparator) {
		/*
		 * TODO Implement
		 */
		throw new UnsupportedOperationException();
	}

	public T peek() {

		if (!isEmpty()) {
			
			return get( 0 );
			
		} else {
			
			throw new EmptyStackException();
		}
	}

	public T pop() {

		if (!isEmpty()) {
			
			return remove( 0 );
			
		} else {
			
			throw new EmptyStackException();
		}
	}

	public void push(T object) {

		super.add( 0, object );
	}

	public Iterator<T> iterator() {

		return super.iterator();
	}
}
